import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Put,
  UseGuards,
  Request,
} from '@nestjs/common';
import { WorkspaceService } from './workspace.service';
import { CreateWorkspaceDto } from './dto/create-workspace.dto';
import { UpdateWorkspaceDto } from './dto/update-workspace.dto';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { RolesGuard } from '../../guards/roles.guard';
import { PermissionsGuard } from '../../guards/permissions.guard';
import { Roles } from '../../decorators/roles.decorator';
import { Permissions } from '../../decorators/permissions.decorator';
import { WorkspaceResponseDto } from './dto/workspace-response.dto';

@Controller('workspaces')
@ApiTags('工作区管理')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard, PermissionsGuard)
export class WorkspaceController {
  constructor(private readonly workspaceService: WorkspaceService) {}

  @Post()
  @Permissions('workspace:create')
  @ApiOperation({ summary: '创建工作区' })
  @ApiResponse({
    status: 201,
    description: '工作区创建成功',
    type: WorkspaceResponseDto,
  })
  async create(@Body() createWorkspaceDto: CreateWorkspaceDto, @Request() req) {
    return this.workspaceService.create(createWorkspaceDto, req.user.id);
  }

  @Get()
  @Permissions('workspace:view')
  @ApiOperation({ summary: '获取所有工作区' })
  @ApiResponse({
    status: 200,
    description: '返回所有工作区',
    type: [WorkspaceResponseDto],
  })
  async findAll() {
    return this.workspaceService.findAll();
  }

  @Get('my')
  @Permissions('workspace:view')
  @ApiOperation({ summary: '获取我的工作区' })
  @ApiResponse({
    status: 200,
    description: '返回当前用户的工作区',
    type: [WorkspaceResponseDto],
  })
  async findMy(@Request() req) {
    return this.workspaceService.findByUser(req.user.id);
  }

  @Get(':id')
  @Permissions('workspace:view')
  @ApiOperation({ summary: '获取工作区详情' })
  @ApiResponse({
    status: 200,
    description: '返回工作区详情',
    type: WorkspaceResponseDto,
  })
  @ApiResponse({ status: 404, description: '工作区不存在' })
  async findOne(@Param('id') id: string) {
    return this.workspaceService.findOne(id);
  }

  @Put(':id')
  @Permissions('workspace:edit')
  @ApiOperation({ summary: '更新工作区' })
  @ApiResponse({
    status: 200,
    description: '工作区更新成功',
    type: WorkspaceResponseDto,
  })
  @ApiResponse({ status: 404, description: '工作区不存在' })
  async update(
    @Param('id') id: string,
    @Body() updateWorkspaceDto: UpdateWorkspaceDto,
  ) {
    return this.workspaceService.update(id, updateWorkspaceDto);
  }

  @Delete(':id')
  @Permissions('workspace:delete')
  @ApiOperation({ summary: '删除工作区（软删除）' })
  @ApiResponse({ status: 204, description: '工作区删除成功' })
  @ApiResponse({ status: 404, description: '工作区不存在' })
  async remove(@Param('id') id: string) {
    await this.workspaceService.softDelete(id);
    return { message: '工作区删除成功' };
  }

  @Delete(':id/permanent')
  @Roles('系统管理员')
  @Permissions('workspace:delete')
  @ApiOperation({ summary: '永久删除工作区' })
  @ApiResponse({ status: 204, description: '工作区永久删除成功' })
  @ApiResponse({ status: 404, description: '工作区不存在' })
  async permanentDelete(@Param('id') id: string) {
    await this.workspaceService.permanentDelete(id);
    return { message: '工作区永久删除成功' };
  }
} 